"use strict";

const Service = require("egg").Service;
const LOGIN_SECRET = Symbol("Node08_Login_Token");

class JsonWebTokenService extends Service {
    /**
     *
     * @param {Egg.Context} ctx egg - context
     */
    constructor(ctx) {
        super(ctx);
        this.jwt = this.app.jwt;
    }

    signLoginToken(id) {
        const jwt = this.jwt.sign({ sub: id }, "Node08_Login_Token");
        return jwt;
    }

    /**
     *
     * @param {string} jwt 登录Token
     * @returns {object} 解析结果
     */
    verifyLoginToken(jwt) {
        const payload = this.jwt.verify(jwt, "Node08_Login_Token", { ignoreExpiration: true });
        console.log(payload);
        return payload;
    }

}

module.exports = JsonWebTokenService;
